﻿using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.DataAccess;
using BySoft.Server.DataAccess.UserCode;
using OneSoft.DataAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BySoft.Server.Business.UserCode
{
    public partial class BsBOMChangeBusiness
    {
        public static DataTable SelectList(string BOMChangeID, string ProductName, string cpFrom, string cpEnd, int pageIndex, int PageSize, out int m_count)
        {
            return BsBOMChangeDataAccess.SelectList(BOMChangeID, ProductName, cpFrom, cpEnd, pageIndex, PageSize, out m_count);
        }

        public static bool Del(string strCode)
        {
            bool isResult = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    DataAccessCommon.Delete(strCode, "BOMChangeID", "BS_BOMChangeDetail");
                    DataAccessCommon.Delete(strCode, "BOMChangeID", "BS_BOMChange");
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isResult = false;
                    broker.Rollback();
                    throw ex;
                }
            }
            return isResult;
        }

        public static DataTable SelectDialogProduct(string BOMChangeID, string ProductID, string ProductName, string userid, int pageIndex, int PageSize, out int m_count)
        {
            return BsBOMChangeDataAccess.SelectDialogProduct(BOMChangeID, ProductID, ProductName, userid, pageIndex, PageSize, out m_count);
        }

        public static DataTable SelectDialogMaterial(string BOMID, string MaterialID, string MaterialName, int pageIndex, int PageSize, out int m_count)
        {
            return BsBOMChangeDataAccess.SelectDialogMaterial(BOMID, MaterialID, MaterialName, pageIndex, PageSize, out m_count);
        }

        public static bool SaveOrUpdate(BsBomchangeEntity entity, DataTable BOMChangeMaterialDT, string type)
        {
            bool istag = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    if (type == "Save")
                    {
                        DataAccessCommon.Insert(entity, broker);
                    }
                    else if (type == "Update")
                    {
                        DataAccessCommon.UpdateOnly(entity, broker);
                    }
                    //原料
                    if (BOMChangeMaterialDT != null && BOMChangeMaterialDT.Rows.Count > 0)
                    {
                        foreach (DataRow drItem in BOMChangeMaterialDT.Rows)
                        {
                            BsBomchangedetailEntity DetailEntity = new BsBomchangedetailEntity();
                            DetailEntity.BOMChangeID = entity.BOMChangeID;
                            DetailEntity.BOMDetailID = CommonMethod.ConvertToDecimal(drItem["BOMDetailID"]);
                            DetailEntity.MaterialID = CommonMethod.GetString(drItem["MaterialID"]);
                            DetailEntity.ChangeType = CommonMethod.GetString(drItem["ChangeType"]);
                            DetailEntity.Nums = CommonMethod.ConvertToDecimal(drItem["Nums"]);
                            DetailEntity.Remark = CommonMethod.GetString(drItem["Remark"]);
                            string strIsNew = CommonMethod.GetString(drItem["IsNew"]);
                            string strIsDel = CommonMethod.GetString(drItem["IsDel"]);
                            if (strIsNew == "1" && strIsDel != "1")
                            {
                                DataAccessCommon.Insert(DetailEntity, true, broker);
                            }
                            if (strIsNew != "1" && strIsDel != "1")
                            {
                                DetailEntity.BOMChangeDetailID = CommonMethod.ConvertToDecimal(drItem["BOMChangeDetailID"]);
                                DataAccessCommon.UpdateOnly(DetailEntity, broker);
                            }
                            if (strIsNew != "1" && strIsDel == "1")
                            {
                                //删除明细
                                ClsCommDA.Del("BS_BOMChangeDetail", "BOMChangeDetailID", CommonMethod.GetString(drItem["BOMChangeDetailID"]), broker);
                            }
                        }
                    }

                    broker.Commit();
                }
                catch
                {
                    istag = false;
                    broker.Rollback();
                    throw;
                }
                return istag;
            }
        }

        public static DataTable GetDataById(string id)
        {
            return BsBOMChangeDataAccess.GetDataById(id);
        }

        public static bool UpdateBomChangeNum(string strCode)
        {
            return BsBOMChangeDataAccess.UpdateBomChangeNum(strCode);
        }
    }
}
